#!/bin/python3 -W ignore
import cairo
import gi , os

gi.require_version("Gtk", "3.0") 
from gi.repository import Gtk

import sys
from term_notebook import *

from common import confirm

class Window(Gtk.ApplicationWindow):

    def call_back(self,widget):
        print(widget)

    def __init__(self, app):
        super(Window, self).__init__(title="{ JingTerm }", application=app)
        self.set_icon_from_file("images/icon.svg")

        #设置窗体透明，必须设置窗体透明，后面组件才能透明！
        screen = self.get_screen()
        self.visual = screen.get_rgba_visual()
        if self.visual != None and screen.is_composited():
            # print('none visual')
            self.set_visual(self.visual)

        self.set_app_paintable(True)
        self.connect("draw", self.area_draw)

        self.notebook = TermNotebook(self) 
        # 设置Notebook透明
        self.notebook.update_opacity(0.82)

        provider = Gtk.CssProvider()
        style_context = Gtk.StyleContext()
        style_context.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
        provider.load_from_path('app.css')
        # provider.load_from_data(".title-button { padding: 2px; margin:0; border-radius:0;} notebook header tabs tab { padding: 2px;}".encode())

        self.add(self.notebook)
        self.set_default_size(900, 600)
        self.set_decorated(False)
        # self.connect("window-state-event", self.on_windows_state_change)

    def area_draw(self, widget, cr):
        cr.set_source_rgba(.2, .2, .2, 0.6)
        cr.set_operator(cairo.OPERATOR_SOURCE)
        cr.paint()
        cr.set_operator(cairo.OPERATOR_OVER)
        
    def on_windows_state_change(self,window,evt):
        #print('win state changed', window, evt.new_window_state)
        pass

    def do_max(self,widget):
        if self.is_maximized():
            self.unmaximize()
        else:
            self.maximize()

    def do_min(self,widget):
        self.iconify()

    def quitApp(self, par=None):
        if not confirm(self, "确定要退出吗？"):
            return

        app.quit()

class Application(Gtk.Application):

    def __init__(self):
        super(Application, self).__init__()

    def do_activate(self):
        self.win = Window(self)
        self.win.show_all()
        self.win.maximize()

    def do_startup(self):
        Gtk.Application.do_startup(self)

# 确保进入main.py所在目录，因为图标文件等都用的是相对目录，否则会加载不到
app_dir = os.path.dirname(__file__)
os.chdir(app_dir)

app = Application()
app.run(sys.argv)
